

package Object;

import UI.Game;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class ConNgan implements DoiTuong {
    private int chuki = 0;
    private int x;
    private int y;
    private Image img;
    Game game;
    private long timer = System.currentTimeMillis();
    
    private DanhSachPhan lstPhan;
    private DanhSachTrung lstTrung;
    
    private void init() {
        Path p = Paths.get(Paths.get("").toAbsolutePath().toString());
        String pimg = p.toString() + "//src//Images/ngan.png";

        File fimg = new File(pimg);
        try {
            img = ImageIO.read(fimg);
        } catch (IOException ex) {
            Logger.getLogger(GioTrung.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
     private int randInt(int min, int max) {
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
    
    public ConNgan(int _x, int _y, Game _game) {
        init();
        x = _x;
        y = _y;
        game = _game;
        lstPhan = new DanhSachPhan();
        lstTrung = new DanhSachTrung();
    }
    
    @Override
    public void tick() {
        if(x<=0)
            chuki = 0;
        if(x>=428)
            chuki = 1;
        if(chuki == 0) 
            x+=3;
        if(chuki ==1) 
            x-=3;
        
        if(System.currentTimeMillis() - timer > 5000) {
            timer = System.currentTimeMillis();
            int random = randInt(1, 10);
            if(random < 7) {
                lstTrung.addtrung(new Trung(x+10, y+90));
            } else {
                lstPhan.addphan(new Phan(x+10, y+90));
            }
        }
        lstPhan.tick();
        lstTrung.tick();
    }
    
    @Override
    public void render(Graphics g) {
        g.drawImage(img, x, y, null);
        lstPhan.render(g);
        lstTrung.render(g);
    }
    
    @Override
    public int getX() {
        return x;
    }
    
    @Override
    public int getY() {
        return y;
    }
    
    @Override
    public void setX(int _x) {
        x = _x;
    }
    
    @Override
    public void setY(int _y) {
        y = _y;
    }
}
